package atm;

import work.work60.SuperArray;

import javax.swing.*;
import java.awt.*;

/**
 * GUI版本的ATM窗口
 */
public class ATMFrame extends JFrame {
        private Image bgImage = new ImageIcon("images/background.jpg").getImage();
        private LoginPanel loginPanel = new LoginPanel(this);
        private MenuPanel menuPanel = new MenuPanel(this);
        private GetPanel getPanel = new GetPanel(this);
        private SavePanel savePanel = new SavePanel(this);
        private SearchPanel searchPanel = new SearchPanel(this);
        private ChangePwdPanel changePwdPanel = new ChangePwdPanel(this);
        private TransferPanel transferPanel = new TransferPanel(this);

        private CardLayout cardLayout = new CardLayout();

        private SuperArray users = new SuperArray();
        /**
         * 登录成功的用户对象
         */
        private User loginUser;

        public ATMFrame() {
            this.init();
            this.setContentPane(new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    g.drawImage(bgImage, 0, 0, this);
                }
            });
            this.setLayout(cardLayout);

            this.add("login", loginPanel);
            this.add("menu", menuPanel);
            this.add("get", getPanel);
            this.add("save", savePanel);
            this.add("search", searchPanel);
            this.add("changePwd", changePwdPanel);
            this.add("trans", transferPanel);
            this.setSize(1024, 768);
            this.setTitle("ATM");
            this.setResizable(false);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }

        //初始化用户信息
        public void init() {
            users.add(new User("aaaaaa", "111111", 1000));
            users.add(new User("bbbbbb", "222222", 2000));
            users.add(new User("cccccc", "333333", 3000));
            users.add(new User("dddddd", "444444", 4000));
            users.add(new User("eeeeee", "555555", 5000));
        }

        /**
         * 根据卡片名字实现切换功能
         *
         * @param name
         */
        public void switchPanel(String name) {
            cardLayout.show(this.getContentPane(), name);
        }

        public SuperArray getUsers() {
            return users;
        }

        public void setUsers(SuperArray users) {
            this.users = users;
        }

        public User getLoginUser() {
            return loginUser;
        }

        public void setLoginUser(User loginUser) {
            this.loginUser = loginUser;
        }

        public SearchPanel getSearchPanel() {
            return searchPanel;
        }

        public static void main(String[] args) {
            new ATMFrame();
        }
    }
